/*
 * TODO language 
 * Copyright (c) 2010 TODO Team
 * 
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 */

#ifndef TODO_INTERNAL_H
#define TODO_INTERNAL_H
#include <map>
#include <deque>
#include <vector>
#include <string>

namespace todo { namespace vm {
class Engine;
}}

namespace todo {

class Value;
class Type;
struct Data;

// Argument value type
struct ArgValues {
	std::deque<Value*> args;
};

// Argument list type
typedef std::deque<Data*> arglist_t;

struct ArgList {
	arglist_t args;
	
	ArgValues* get_values(todo::vm::Engine*);
	void clear(void);
};

// Vector type representation
typedef std::vector<Value*> todo_vector;

// Map type representation
typedef std::map<std::string,Value*> todo_map;

/**
 * The variable representation
 */
struct Var {
	// The variable name
	const std::string name;
	// Flag indicates if it should be freed
	bool free;
	// The variable type
	const Type* type;
	// The variable data
	Data* data;
	
	Var(const std::string& name_, const Type* type_, Data* data_) :
		name(name_), free(false), type(type_), data(data_) { }
};

struct FunctionArg {
	const std::string name;
	const Type* type;
	
	FunctionArg() { }
	
	FunctionArg(std::string& name_, const Type* type_) :
		name(name_), type(type_) { }	
};

typedef std::deque<FunctionArg*> funcargs_t;

struct FunctionArgList {
	funcargs_t args;
	
	void clear(void);
};

struct Function {
	std::string* name;
	unsigned int num_params;
	unsigned int jmp_start, jmp_end;
	funcargs_t* args;
	
	Function(std::string* name_, unsigned int jmp_start_, unsigned int jmp_end_, funcargs_t* args_) :
		name(name_), num_params(0), jmp_start(jmp_start_), jmp_end(jmp_end_), args(args_) { }
};

typedef std::map<std::string, Var*> variable_map;
typedef std::map<std::string, Function*> func_map;

/**
 * Scope representation
 */
class Scope {
public:
	std::string name;
	// The variables
	variable_map variables;
	// The functions
	func_map functions;
	
	Scope() { }
	Scope(const std::string& name_) : name(name_) { }
	~Scope();
};

class Scopes {
public:	
	typedef std::vector<Scope*> scopes_t;
	Scope* Gscope; // Global scope
	Scope* Cscope; // Current scope
	
	void add(Scope*);
	void add_var(Var*);
	Var* get_var(const std::string&);
	void pop(void);
	void set_global(Scope*);
	
	Scopes() { }
	~Scopes();
private:
	scopes_t scopes;
};

// Method/Property flags
enum MemberFlags {
	TODO_PUBLIC     = 1<<0,
	TODO_PROTECTED  = 1<<1,
	TODO_PRIVATE    = 1<<2,
	TODO_STATIC     = 1<<3,
	TODO_FINAL      = 1<<4,
	TODO_VIRTUAL    = 1<<5
};

// Method prototype
typedef Value* (Type::*MethodPtr)(Value*, const ArgValues*);

/*
 * @brief Internal method representation class.
 *
 * This class used to be the base class of Method and StaticMethod
 * but those got deprecated for simplicity's sake.
 */
struct Method {
	// Method name
	std::string name;

	// Method flags (access, visibility)
	MemberFlags flags;

	// A pointer to the method
	MethodPtr   ptr;
	
	// Return type
	Type*       return_type;

	// Amount of parameters
	const int   num_params;

	// Amount of required parameters
	const int   num_required;

	// Array of parameter types
	Type**      params;
	
	Method(MemberFlags, const std::string&, const std::string&, MethodPtr, int = 0, Type** = NULL, int = -1);
	Method(MemberFlags, const std::string&, Type*, MethodPtr, int = 0, Type** = NULL, int = -1);
};

} // todo

#endif // TODO_INTERNAL_H
